/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client.widgets;

import java.util.ArrayList;
import java.util.List;

import org.nuxeo.gwt.habyt.app.client.Application;
import org.nuxeo.gwt.habyt.app.client.View;
import org.nuxeo.gwt.habyt.invocation.client.DataModel;
import org.nuxeo.gwt.habyt.invocation.client.Referenceable;
import org.nuxeo.gwt.habyt.templates.client.TemplatePanel;
import org.nuxeo.gwt.habyt.templates.client.bindings.Helper;

import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Experimental - work in progress
 * 
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class TabbedViewWidget extends View {

    protected List<Referenceable> inputs;

    public TabbedViewWidget() {
        inputs = new ArrayList<Referenceable>();
        initDefaults();
    }

    @Override
    protected Widget createWidget() {
        return new TabPanel();
    }

    public TabPanel getPanel() {
        return (TabPanel) getWidget();
    }

    protected void initDefaults() {
        setSize("100%", "100%");
        // getDeckPanel().setSize("100%", "100%");
    }

    protected Widget createContent(Referenceable input) {
        TemplatePanel panel = new TemplatePanel();
        panel.load(getActiveInput().getRef());
        return panel;
    }

    @Override
    public void doOpen(Referenceable ref) {
        if (ref == null) {
            close();
            return;
        }
        int len = inputs.size();
        for (int i = 0; i < len; i++) {
            Referenceable in = inputs.get(i);
            if (in.getRef().sameAs(ref)) {
                getPanel().selectTab(i);
                // TODO change the tab content
                return;
            }
        }
        Widget widget = createContent(ref);
        widget.setSize("100%", "100%");
        getPanel().add(widget, getInputTitle(ref), true);
        inputs.add(ref);
        getPanel().selectTab(getPanel().getTabBar().getTabCount() - 1);
    }

    public String getInputTitle(Referenceable input) {
        if (input instanceof DataModel) {
            return ((DataModel) input).getTitle();
        }
        return input.getRef().getId();
    }

    public Referenceable getActiveInput() {
        int tab = getPanel().getTabBar().getSelectedTab();
        return tab > -1 ? inputs.get(tab) : null;
    }

    public Referenceable[] getInputs() {
        return inputs.toArray(new Referenceable[inputs.size()]);
    }

    public void close() {
        int tab = getPanel().getTabBar().getSelectedTab();
        if (tab > -1) {
            // TODO if view dirty show save prompt
            getPanel().getTabBar().removeTab(tab);
            int last = getPanel().getTabBar().getTabCount() - 1;
            if (last > -1) {
                getPanel().getTabBar().selectTab(tab > last ? last : tab);
            }
        }
    }

    public boolean hasInput() {
        return getPanel().getTabBar().getTabCount() > 0;
    }

    public void closeAll() {
        while (getPanel().getTabBar().getTabCount() > 0) {
            close();
        }
    }

    public void reloadActiveView() {
        Referenceable input = getActiveInput();
        if (input != null) {
            // setWidget(input.getViewFactory().getView(input.getRef()));
            // TODO
        }
    }

    @Override
    public String getId() {
        return Helper.getIdAttribute(getElement());
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        if (getId() != null) {
            Application.get().addView(this);
        }
    }

    @Override
    protected void onUnload() {
        super.onUnload();
        if (getId() != null) {
            Application.get().removeView(this);
        }
    }

}
